package modelo;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import entities.AreaEntity;
import entities.MozoEntity;
import entities.ProductoEntity;
import entities.TipoProductoEntity;
import hbt.AbstractHibernateManager;

public class AreaDao extends AbstractHibernateManager {

	public static AreaDao instancia;
	
	public static AreaDao getInstancia(){
		if (instancia == null)
			instancia = new AreaDao();
		return instancia;
	}
	
	
	public void save(AreaEntity area){
		Session s= null;
		try {
			s=getNewSession();
			s.beginTransaction();
			s.persist(area);
			s.flush();
			s.getTransaction().commit();
			System.out.println("[area PERSISTED ON TABLE areas]::OK");
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			closeSession(s);
		}
	}
	
	public void guardar(AreaEntity area){
		Session s= null;
		try {
			s=getNewSession();
			s.beginTransaction();
			s.merge(area);
			s.flush();
			s.getTransaction().commit();
			System.out.println("[area SAVED ON TABLE areas]::OK");
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			closeSession(s);
		}
	}
	
	public AreaEntity obtenerArea(Integer id) {
		Session s = null;
		AreaEntity areaE = new AreaEntity();
		
		try {
			s = getNewSession();
			areaE =  (AreaEntity) s.createQuery("FROM AreaEntity area " +
									"WHERE area.id = :id")
							  .setInteger("id", id).uniqueResult();
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally{
			closeSession(s);
		}
		return areaE;
	}

	
}
